import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import axios from 'axios'

// 配置axios
axios.defaults.baseURL = 'http://localhost:5000/api'
axios.interceptors.request.use(config => {
  const token = localStorage.getItem('token')
  if (token) {
    config.headers['x-auth-token'] = token
  }
  return config
})

// 全局错误处理
axios.interceptors.response.use(
  response => response,
  error => {
    const { status } = error.response || {}
    if (status === 401) {
      // 未授权，清除token并重定向到登录页
      store.dispatch('auth/logout')
      router.push('/login')
    }
    return Promise.reject(error)
  }
)

const app = createApp(App)

app.use(store)
app.use(router)
app.use(ElementPlus)

app.mount('#app') 